Print Statement in Java
Java provides 3 different functions to print the text to the screen.
print()
println()
printf()
The print() function
The print()
function is used to print the text to the screen. It does not add a new line to the output's end.
Anything that is enclosed in double quotes(""
) is considered as a string or text.
If there is no double quotes(""
) syntax, then error will be thrown by the compiler.
Example
System.out.print("print() function.");
System.out.print("Will print in the same line without break.");
Output
print() function.Will print in the same line without break.
The println() function
The println()
function is used to print the text to the screen and a new line will be added.
Example
System.out.print("print() function.");
System.out.print("Will print in the same line without break.");
Output
print() function.
Will print in the same line without break.
The printf() function
The printf()
function is used to print a formated output.
Syntax
System.out.printf(format, arguments);
Example
System.out.printf("Welcome to %s","Java");
Output
Welcome to Java
Here %s
is an access specifier for a string. Here, the string value will replace that %s
which is Java
.